<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Support\Facades\Route;
use Laminas\Diactoros\ResponseFactory;
use Laminas\Diactoros\ServerRequestFactory;
use Laminas\Diactoros\StreamFactory;
use Laminas\Diactoros\UploadedFileFactory;
use Laravel\Passport\Http\Middleware\CheckCredentials;
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;

class CheckClientCredentials extends CheckCredentials
{
    public function handle($request, Closure $next, ...$scopes)
    {
        $urlArr = [
            'api/client/common/video-notify',
        ];
        $route = Route::current()->uri();
        if(in_array($route, $urlArr))  return $next($request);

        $psr = (new PsrHttpFactory(
            new ServerRequestFactory,
            new StreamFactory,
            new UploadedFileFactory,
            new ResponseFactory
        ))->createRequest($request);

        try
        {
            $psr = $this->server->validateAuthenticatedRequest($psr);
        }
        catch (\Exception $e)
        {
            return Response()->json([
                'returnMsg' => '无效的token',
                'isSuccess'    => false,
                'code'    => 40000,
            ], 200);
        }

        return $next($request);
    }

    protected function validateCredentials($token)
    {
    }

    protected function validateScopes($token, $scopes)
    {
    }
}
